Paillier Homomorphic Encryption Example

DISCLAIMER: This is a proof-of-concept implementation. It does not represent a remotely product ready implementation or follow proper conventions for security, convenience, or scalability. It is part of a broader proof-of-concept demonstrating the vision of the OpenMined project, its major moving parts, and how they might work together.


In [10]:
from syft.he.paillier import KeyPair, PaillierTensor
from syft import TensorBase
import numpy as np

Basic Ops


In [11]:
pubkey,prikey = KeyPair().generate()

In [12]:
x = PaillierTensor(pubkey, np.array([1, 2, 3, 4, 5.]))

In [15]:
x.decrypt(prikey)


Out[15]:
array([ 1.,  2.,  3.,  4.,  5.])

In [20]:
(x+x[0]).decrypt(prikey)


Out[20]:
array([ 2.,  3.,  4.,  5.,  6.])

In [21]:
(x*5).decrypt(prikey)


Out[21]:
array([  5.,  10.,  15.,  20.,  25.])

In [22]:
(x+x/5).decrypt(prikey)


Out[22]:
array([ 1.2,  2.4,  3.6,  4.8,  6. ])

Key SerDe


In [23]:
pubkey,prikey = KeyPair().generate()

In [24]:
x = PaillierTensor(pubkey, np.array([1, 2, 3, 4, 5.]))

In [25]:
pubkey_str = pubkey.serialize()
prikey_str = prikey.serialize()

In [26]:
pubkey2,prikey2 = KeyPair().deserialize(pubkey_str,prikey_str)

In [27]:
prikey2.decrypt(x)


Out[27]:
array([ 1.,  2.,  3.,  4.,  5.])

In [28]:
y = PaillierTensor(pubkey,(np.ones(5))/2)

In [29]:
prikey.decrypt(y)


Out[29]:
array([ 0.5,  0.5,  0.5,  0.5,  0.5])

Value SerDe


In [30]:
import pickle

In [31]:
y_str = pickle.dumps(y)

In [32]:
y2 = pickle.loads(y_str)

In [34]:
prikey.decrypt(y2)


Out[34]:
array([ 0.5,  0.5,  0.5,  0.5,  0.5])

In [ ]: